package edu.sandau.basic.controller;

import edu.sandau.basic.domain.role;
import edu.sandau.basic.global.GlobalEnum;
import edu.sandau.basic.service.RoleService;
import edu.sandau.basic.vo.BasicMsg;
import edu.sandau.basic.vo.BasicMsgWithData;
import edu.sandau.basic.vo.BasicMsgWithRoleData;
import java.util.List;
import java.util.ArrayList;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/api")
@Tag(name="Role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @PostMapping("/role")
    @Operation(summary = "Add a new role.")
    public BasicMsg addRole(@RequestBody role role) {
        BasicMsg msg = new BasicMsg();
        GlobalEnum.ErrorCode errorCode = roleService.addRole(role);
        msg.setErrorCode(errorCode);

        if (errorCode == GlobalEnum.ErrorCode.OK) {
            msg.setErrorMsg("添加角色成功。");
            msg.setOperationResult(GlobalEnum.OperationResult.SUCC);
        } else {
            msg.setErrorMsg("添加角色失败。");
            msg.setOperationResult(GlobalEnum.OperationResult.UNSUCC);
        }

        return msg;
    }

    @GetMapping("/role")
    @Operation(summary = "Get all role information.")
    public BasicMsgWithRoleData getAllRoles() {
        BasicMsgWithRoleData msg = new BasicMsgWithRoleData();
        List<role> roles = roleService.getAllRoles();

        if (roles != null && !roles.isEmpty()) {
            // 只设置data，避免重复数据
            msg.setData(roles);
            msg.setErrorMsg("获取角色信息成功，共" + roles.size() + "个角色。");
            msg.setOperationResult(GlobalEnum.OperationResult.SUCC);
        } else {
            msg.setErrorMsg("系统中没有角色数据。");
            msg.setOperationResult(GlobalEnum.OperationResult.UNSUCC);
        }

        return msg;
    }

    @DeleteMapping("/role")
    @Operation(summary = "Delete a role by name.")
    public BasicMsg deleteRoleByName(@RequestParam String name) {
        BasicMsg msg = new BasicMsg();

        if (name == null || name.trim().isEmpty()) {
            msg.setErrorMsg("角色名不能为空。");
            msg.setOperationResult(GlobalEnum.OperationResult.UNSUCC);
            return msg;
        }

        // 调用RoleService删除角色
        GlobalEnum.ErrorCode errorCode = roleService.deleteRoleByName(name);

        if (errorCode == GlobalEnum.ErrorCode.OK) {
            msg.setErrorMsg("删除角色成功。");
            msg.setOperationResult(GlobalEnum.OperationResult.SUCC);
        } else {
            msg.setErrorMsg("删除角色失败，可能角色不存在或有其他错误。");
            msg.setOperationResult(GlobalEnum.OperationResult.UNSUCC);
            msg.setErrorCode(errorCode);
        }

        return msg;
    }

    @PutMapping("/role/{rolecode}")
    @Operation(summary = "Update a role by rolecode.")
    public BasicMsg updateRoleByRolecode(@PathVariable String rolecode, @RequestBody role role){
        BasicMsg msg = new BasicMsg();
        GlobalEnum.ErrorCode errorCode = roleService.updateRoleByRolename(rolecode, role);
        msg.setErrorCode(errorCode);
        if(errorCode == GlobalEnum.ErrorCode.OK){
            msg.setErrorMsg("角色 " + rolecode + " 信息更新成功。");
            msg.setOperationResult(GlobalEnum.OperationResult.SUCC);
        } else if(errorCode == GlobalEnum.ErrorCode.USER_NOT_EXIST){
            msg.setErrorMsg("角色 " + rolecode + " 不存在。");
            msg.setOperationResult(GlobalEnum.OperationResult.UNSUCC);
        } else {
            msg.setErrorMsg("更新角色信息失败。");
            msg.setOperationResult(GlobalEnum.OperationResult.UNSUCC);
        }
        return msg;
    }

    @GetMapping("/role/search")
    @Operation(summary = "Search for a role by name (rolecode).")
    public BasicMsgWithRoleData searchRoleByName(@RequestParam String name) {
        BasicMsgWithRoleData msg = new BasicMsgWithRoleData();

        if (name == null || name.trim().isEmpty()) {
            msg.setErrorMsg("角色名不能为空。");
            msg.setOperationResult(GlobalEnum.OperationResult.UNSUCC);
            return msg;
        }

        // 调用RoleService查询角色
        role role = roleService.getRoleByName(name);

        if (role != null) {
            // 将单个角色对象包装在列表中
            List<role> roleList = new ArrayList<>();
            roleList.add(role);
            msg.setData(roleList);
            msg.setErrorMsg("查询角色成功。");
            msg.setOperationResult(GlobalEnum.OperationResult.SUCC);
        } else {
            msg.setErrorMsg("未找到名为" + name + "的角色。");
            msg.setOperationResult(GlobalEnum.OperationResult.UNSUCC);
        }

        return msg;
    }
}

